package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.UserData;
import com.xijia.wuyouwallet.utils.SharedpreferncesUtil;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/14 0014.
 */

public class RegisterSuccessActivity extends BaseActivity implements View.OnClickListener{
    private ImageView register_success_back;
    private TextView register_success_attestation;
    private TextView register_success_latter;
    private String phone,psd;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_success);
        initView();
    }
    private void initView(){
        register_success_attestation = (TextView) findViewById(R.id.register_success_attestation);
        register_success_attestation.setOnClickListener(this);
        register_success_latter = (TextView) findViewById(R.id.register_success_latter);
        register_success_latter.setOnClickListener(this);
        register_success_back = (ImageView) findViewById(R.id.register_success_back);
        register_success_back.setOnClickListener(this);
        phone = getIntent().getStringExtra("phone");
        psd = getIntent().getStringExtra("pasword");
        //LoginActivity.finsh();
        login();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.register_success_back:
                finish();
                break;
            case R.id.register_success_attestation:
                MyApp.isAttentionJump = true;
                jumpToPage(AttestationActivity.class);
                finish();
                break;
            case R.id.register_success_latter:
                jumpToPage(HomeActivity.class);
                finish();
                break;
        }
    }
    private void login(){
        Map<String,Object> map = new HashMap<>();
        map.put("login_name",phone);
        map.put("login_passwd",psd);
        XUtil.Post(UrlConfig.LOGIN_URL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyApp.userData = JSONObject.parseObject(result,UserData.class);
                if (MyApp.userData.getCode()==200){
                    SharedpreferncesUtil.putBoolean("is_login", true, RegisterSuccessActivity.this);
                    SharedpreferncesUtil.putString("user_phone", phone, RegisterSuccessActivity.this);
                    SharedpreferncesUtil.putString("user_pwd", psd, RegisterSuccessActivity.this);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                Log.e("info","登录失败");
            }
        });
    }
}
